
<template>
  <!-- 总容器 -->
  <div class="wrapper">
    <!-- header部分 -->
    <header>
      <div class="icon-location-box">
        <div class="icon-location"></div>
      </div>

      <div class="location-text">
        天津大学北洋园校区<i class="fa fa-caret-down"></i>
      </div>
    </header>

    <!-- search部分 -->
    <div class="search">
      <div class="search-fixed-top" ref="fixedBox">
        <div class="search-box">
          <i class="fa fa-search"></i>
          <input
            type="text"
            v-model="searchQuery"
            placeholder="搜索饿了么商家、商品名称"
          />
          <button class="button" @click="handleSearch">搜索</button>
        </div>
      </div>
    </div>

    <!-- 点餐分类部分 -->
    <ul class="foodtype">
      <li @click="toBusinessList(1)">
        <img src="../assets/dcfl01.png" />
        <p>美食</p>
      </li>
      <li @click="toBusinessList(2)">
        <img src="../assets/dcfl02.png" />
        <p>早餐</p>
      </li>
      <li @click="toBusinessList(3)">
        <img src="../assets/dcfl03.png" />
        <p>跑腿代购</p>
      </li>
      <li @click="toBusinessList(4)">
        <img src="../assets/dcfl04.png" />
        <p>汉堡披萨</p>
      </li>
      <li @click="toBusinessList(5)">
        <img src="../assets/dcfl05.png" />
        <p>甜品饮品</p>
      </li>
      <li @click="toBusinessList(6)">
        <img src="../assets/dcfl06.png" />
        <p>速食简餐</p>
      </li>
      <li @click="toBusinessList(7)">
        <img src="../assets/dcfl07.png" />
        <p>地方小吃</p>
      </li>
      <li @click="toBusinessList(8)">
        <img src="../assets/dcfl08.png" />
        <p>米粉面馆</p>
      </li>
      <li @click="toBusinessList(9)">
        <img src="../assets/dcfl09.png" />
        <p>包子粥铺</p>
      </li>
      <li @click="toBusinessList(10)">
        <img src="../assets/dcfl10.png" />
        <p>炸鸡炸串</p>
      </li>
    </ul>

    <!-- 横幅广告部分 -->
    <div class="banner">
      <h3>品质套餐</h3>
      <p>搭配齐全吃得好</p>
      <a>立即抢购 &gt;</a>
    </div>

    <!-- 超级会员部分 -->
    <div class="supermember">
      <div class="left">
        <img src="../assets/super_member.png" />
        <h3>超级会员</h3>
        <p>&#8226; 每月享超值权益</p>
      </div>
      <div class="right">立即开通 &gt;</div>
    </div>

    <!-- 推荐商家部分 -->
    <div class="recommend">
      <div class="recommend-line"></div>
      <p>推荐商家</p>
      <div class="recommend-line"></div>
    </div>

    <!-- 推荐方式部分 -->
    <ul class="recommendtype">
      <li>综合排序<i class="fa fa-caret-down"></i></li>
      <li>距离最近</li>
      <li>销量最高</li>
      <li>筛选<i class="fa fa-filter"></i></li>
    </ul>

    <!-- 推荐商家列表部分 -->
    <ul class="business">
      <BusinessItem
        v-for="item in businessArr"
        :key="item.businessId"
        :business="item"
        @business-info="toBusinessInfo"
      ></BusinessItem>
    </ul>

    <!-- 底部菜单部分 -->
    <Footer></Footer>
  </div>
</template>

<script>
import { ref, onMounted, onUnmounted } from 'vue';
import { useRouter } from 'vue-router'; // 导入 useRouter
import Footer from '../components/Footer.vue'
import BusinessItem from '@/components/BusinessItem.vue'
import axios from 'axios'

export default {
  name: 'Index',
  components: {
    Footer,
    BusinessItem
  },
  setup() {
    const router = useRouter(); // 获取路由实例
    const fixedBox = ref(null);
    const searchQuery = ref('');
    const businessArr = ref([]);
    const user = ref({});

    const orderTypeId = ref(1); // 假设你有一个响应式的 orderTypeId

    const toBusinessList = (orderTypeId) => {
      router.push({
        path: '/businessList',
        query: { orderTypeId: orderTypeId },
      })
    };

    const fetchBusinessList = async () => {
      try {
        const response = await axios.get(`/businesses/orderType/${orderTypeId.value}`);
        businessArr.value = response.data.data;
        if (user.value != null) {
          await listCart();
        }
      } catch (error) {
        console.error(error);
      }
    };

    const listCart = async () => {
      try {
        const response = await axios.get(`/carts?userId=${user.value.userId}`);
        let cartArr = response.data.data;
        for (let businessItem of businessArr.value) {
          businessItem.quantity = 0;
          for (let cartItem of cartArr) {
            if (cartItem.businessId === businessItem.businessId) {
              businessItem.quantity += cartItem.quantity;
            }
          }
        }
      } catch (error) {
        console.error(error);
      }
    };

    const toBusinessInfo = (businessId) => {
      router.push({
        path: '/businessInfo',
        query: {
          businessId: businessId,
        },
      })
    };

    const handleSearch = async () => {
      try {
        const response = await axios.get(`/businesses/search/${encodeURIComponent(searchQuery.value)}`);
        if (response.data.state === 200) {
          businessArr.value = response.data.data;
          router.push({
            path: '/businessList',
            query: { businessArr: JSON.stringify(businessArr.value) },
          });
        } else {
          alert(response.data.message);
        }
      } catch (error) {
        console.error('搜索出错:', error);
      }
    };

    onMounted(() => {
      user.value = JSON.parse(sessionStorage.getItem('user'));
      fetchBusinessList();
      document.onscroll = () => {
        let scroll = document.documentElement.scrollTop || document.body.scrollTop;
        let width = document.documentElement.clientWidth;
        if (scroll > width * 0.12) {
          fixedBox.value.style.position = 'fixed';
          fixedBox.value.style.left = '0';
          fixedBox.value.style.top = '0';
        } else {
          fixedBox.value.style.position = 'static';
        }
      };
    });

    onUnmounted(() => {
      document.onscroll = null;
    });

    return {
      fixedBox,
      searchQuery,
      businessArr,
      user,
      orderTypeId,
      toBusinessList,
      fetchBusinessList,
      listCart,
      toBusinessInfo,
      handleSearch
    };
  }
}
</script>
<style>
.wrapper {
  width: 100%;
  padding-bottom: 14vw;
}
.wrapper header {
  width: 100%;
  height: 12vw;
  background-color: #0097ff;
  display: flex;
  align-items: center;
  z-index: 1000;
}
.wrapper header .icon-location-box {
  width: 3.5vw;
  height: 3.5vw;
  margin: 0 1vw 0 3vw;
}
.wrapper header .location-text {
  font-size: 4.5vw;
  font-weight: 700;
  color: #fff;
}
.wrapper header .location-text .fa-caret-down {
  margin-left: 1vw;
}
.wrapper .search {
  width: 100%;
  height: 13vw;
  z-index: 1000;
}
.wrapper .search .search-fixed-top {
  width: 100%;
  height: 13vw;
  background-color: #0097ff;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}
.wrapper .search .search-fixed-top .search-box {
  width: 90%;
  height: 9vw;
  background-color: #fff;
  border-radius: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 3.5vw;
  color: #aeaeae;
  font-family: '宋体';
  user-select: none;
}
.wrapper .search .search-fixed-top .search-box input[type='text'] {
  border: none;
  flex-grow: 1;
  text-align: left;
  user-select: none;
  margin-left: 1vw;
  font-family: 'Arial', sans-serif;
}
.wrapper .search .search-fixed-top .search-box .button {
  background-color: #0097ff;
  border-radius: 20px;
  width: 15vw;
  height: 8vw;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  cursor: pointer;
  border: none;
  outline: none;
  margin-right: 1px;
}
.wrapper .search .search-fixed-top .search-box .fa-search {
  margin-left: 2vw;
}
.wrapper .foodtype {
  width: 100%;
  height: 48vw;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  align-content: center;
}
.wrapper .foodtype li {
  width: 18vw;
  height: 20vw;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  user-select: none;
  cursor: pointer;
}
.wrapper .foodtype li img {
  width: 12vw;
  height: 10.3vw;
}
.wrapper .foodtype li p {
  font-size: 3.2vw;
  color: #666;
}
.wrapper .banner {
  width: 95%;
  margin: 0 auto;
  height: 29vw;
  background-image: url(../assets/whitebackground.png);
  background-repeat: no-repeat;
  background-size: cover;
  box-sizing: border-box;
  padding: 2vw 6vw;
}
.wrapper .banner h3 {
  font-size: 4.2vw;
  margin-bottom: 1.2vw;
}
.wrapper .banner p {
  font-size: 3.4vw;
  color: #666;
  margin-bottom: 2.4vw;
}
.wrapper .banner a {
  font-size: 3vw;
  color: #c79060;
  font-weight: 700;
}
.wrapper .supermember {
  width: 95%;
  margin: 0 auto;
  height: 11.5vw;
  background-color: #feedc1;
  margin-top: 1.3vw;
  border-radius: 2px;
  color: #644f1b;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.wrapper .supermember .left {
  display: flex;
  align-items: center;
  margin-left: 4vw;
  user-select: none;
}
.wrapper .supermember .left img {
  width: 6vw;
  height: 6vw;
  margin-right: 2vw;
}
.wrapper .supermember .left h3 {
  font-size: 4vw;
  margin-right: 2vw;
}
.wrapper .supermember .left p {
  font-size: 3vw;
}
.wrapper .supermember .right {
  font-size: 3vw;
  margin-right: 4vw;
  cursor: pointer;
}
.wrapper .recommend {
  width: 100%;
  height: 14vw;
  display: flex;
  justify-content: center;
  align-items: center;
}
.wrapper .recommend .recommend-line {
  width: 6vw;
  height: 0.2vw;
  background-color: #888;
}
.wrapper .recommend p {
  font-size: 4vw;
  margin: 0 4vw;
}
.wrapper .recommendtype {
  width: 100%;
  height: 5vw;
  margin-bottom: 5vw;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.wrapper .recommendtype li {
  font-size: 3.5vw;
  color: #555;
}
.wrapper .business {
  width: 100%;
}
.wrapper .business li {
  width: 100%;
  box-sizing: border-box;
  padding: 2.5vw;
  user-select: none;
  border-bottom: solid 1px #ddd;
  display: flex;
}
.wrapper .business li img {
  width: 18vw;
  height: 18vw;
}
.wrapper .business li .business-info {
  width: 100%;
  box-sizing: border-box;
  padding-left: 3vw;
}
.wrapper .business li .business-info .business-info-h {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2vw;
}
.wrapper .business li .business-info .business-info-h h3 {
  font-size: 4vw;
  color: #333;
}
.wrapper .business li .business-info .business-info-h .business-info-like {
  width: 1.6vw;
  height: 3.4vw;
  background-color: #666;
  color: #fff;
  font-size: 4vw;
  margin-right: 4vw;
  display: flex;
  justify-content: center;
  align-items: center;
}
.wrapper .business li .business-info .business-info-star {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2vw;
  font-size: 3.1vw;
}
.wrapper
  .business
  li
  .business-info
  .business-info-star
  .business-info-star-left {
  display: flex;
  align-items: center;
}
.wrapper
  .business
  li
  .business-info
  .business-info-star
  .business-info-star-left
  .fa-star {
  color: #fec80e;
  margin-right: 0.5vw;
}
.wrapper
  .business
  li
  .business-info
  .business-info-star
  .business-info-star-left
  p {
  color: #666;
  margin-left: 1vw;
}
.wrapper
  .business
  li
  .business-info
  .business-info-star
  .business-info-star-right {
  background-color: #0097ff;
  color: #fff;
  font-size: 2.4vw;
  border-radius: 2px;
  padding: 0 0.6vw;
}
.wrapper .business li .business-info .business-info-delivery {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2vw;
  color: #666;
  font-size: 3.1vw;
}
.wrapper .business li .business-info .business-info-explain {
  display: flex;
  align-items: center;
  margin-bottom: 3vw;
}
.wrapper .business li .business-info .business-info-explain div {
  border: solid 1px #ddd;
  font-size: 2.8vw;
  color: #666;
  border-radius: 3px;
  padding: 0 0.1vw;
}
.wrapper .business li .business-info .business-info-promotion {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.8vw;
}
.wrapper
  .business
  li
  .business-info
  .business-info-promotion
  .business-info-promotion-left {
  display: flex;
  align-items: center;
}
.wrapper
  .business
  li
  .business-info
  .business-info-promotion
  .business-info-promotion-left
  .business-info-promotion-left-incon {
  width: 4vw;
  height: 4vw;
  background-color: #70bc46;
  border-radius: 3px;
  font-size: 3vw;
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
}
.wrapper
  .business
  li
  .business-info
  .business-info-promotion
  .business-info-promotion-left
  p {
  color: #666;
  font-size: 3vw;
  margin-left: 2vw;
}
.wrapper
  .business
  li
  .business-info
  .business-info-promotion
  .business-info-promotion-right {
  display: flex;
  align-items: center;
  font-size: 2.5vw;
  color: #999;
}
.wrapper
  .business
  li
  .business-info
  .business-info-promotion
  .business-info-promotion-right
  p {
  margin-right: 2vw;
}
</style>
